home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / status.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  6.4 KB  |  322 lines

  1. /* status.c -- A simple status line management file.  */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  */
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27.  
  28. #ifdef HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #else /* !HAVE_STDLIB_H */
  31. #include "ansi_stdlib.h"
  32. #endif /* !HAVE_STDLIB_H */
  33.  
  34. #ifdef HAVE_UTSNAME
  35. #include <sys/utsname.h>
  36. #endif
  37.  
  38. #include "xtime.h"
  39.  
  40. #include "xstring.h"
  41. #include "xmalloc.h"
  42. #include "window.h"
  43. #include "status.h"
  44. #include "configure.h"
  45. #include "tty.h"
  46. #include "misc.h"
  47.  
  48.  
  49. extern int AnsiColors;
  50.  
  51.  
  52. static window_t *status_win = NULL;
  53. static char *stemp;
  54. static int columns;
  55. static int line;
  56. static char *default_msg;
  57.  
  58. #ifdef HAVE_UTSNAME
  59. static struct utsname u;
  60. #endif
  61.  
  62.  
  63. #define STATUSBAR_FIELDS        9
  64.  
  65. static char *StatusBarFields[STATUSBAR_FIELDS] =
  66. {
  67.     "StatusBarForeground",
  68.     "StatusBarBackground",
  69.     "StatusBarBrightness",
  70.     "StatusBarWarningForeground",
  71.     "StatusBarWarningBackground",
  72.     "StatusBarWarningBrightness",
  73.     "StatusBarErrorForeground",
  74.     "StatusBarErrorBackground",
  75.     "StatusBarErrorBrightness"
  76. };
  77.  
  78. #ifdef HAVE_LINUX
  79. static int StatusBarColors[STATUSBAR_FIELDS] =
  80. {
  81.     BLACK, CYAN, OFF, BLACK, WHITE, OFF, WHITE, RED, ON
  82. };
  83. #else   /* !HAVE_LINUX */
  84. static int StatusBarColors[STATUSBAR_FIELDS] =
  85. {
  86.     BLACK, WHITE, OFF, BLACK, WHITE, OFF, BLACK, WHITE, ON
  87. };
  88. #endif  /* !HAVE_LINUX */
  89.  
  90. #define StatusBarForeground             StatusBarColors[0]
  91. #define StatusBarBackground             StatusBarColors[1]
  92. #define StatusBarBrightness             StatusBarColors[2]
  93. #define StatusBarWarningForeground      StatusBarColors[3]
  94. #define StatusBarWarningBackground      StatusBarColors[4]
  95. #define StatusBarWarningBrightness      StatusBarColors[5]
  96. #define StatusBarErrorForeground        StatusBarColors[6]
  97. #define StatusBarErrorBackground        StatusBarColors[7]
  98. #define StatusBarErrorBrightness        StatusBarColors[8]
  99.  
  100.  
  101. void
  102. status_init(_columns, _begin_y, def_msg)
  103.     int _columns, _begin_y;
  104.     char *def_msg;
  105. {
  106.     use_section(AnsiColors ? cSection : bwSection);
  107.  
  108.     get_colorset_var(StatusBarColors, StatusBarFields, STATUSBAR_FIELDS);
  109.  
  110.  
  111.     columns     = _columns;
  112.     line        = _begin_y;
  113.     default_msg = def_msg;
  114.     status_win  = window_init(0, _begin_y, 1, _columns);
  115.     stemp       = xmalloc(columns);
  116.  
  117. #ifdef HAVE_UTSNAME
  118.     uname(&u);
  119. #endif
  120. }
  121.  
  122.  
  123. void
  124. status_end()
  125. {
  126.     window_end(status_win);
  127. }
  128.  
  129.  
  130. static void
  131. build_msg(msg_name, center)
  132.    char *msg_name;
  133.    int center;
  134. {
  135.     int i, j;
  136.     struct tm *time;
  137.     char date_str[32];
  138.     char *ptr, *temp_msg;
  139.     size_t len, temp_msg_len;
  140.  
  141.  
  142.     memset(stemp, ' ', columns);
  143.  
  144.     if (msg_name == NULL)
  145.     msg_name = default_msg;
  146.  
  147.     temp_msg = xmalloc(temp_msg_len = (strlen(msg_name) + 1));
  148.  
  149.     for (i = 0, j = 0; msg_name[i]; i++)
  150.     if (msg_name[i] == '\\')
  151.         switch (msg_name[i + 1])
  152.         {
  153.         case 'h' :
  154.  
  155. #ifdef HAVE_UTSNAME
  156.             ptr = u.nodename;
  157. #else
  158.             ptr = "";
  159. #endif
  160.             goto get_system_info;
  161.  
  162.         case 's' :
  163.  
  164. #ifdef HAVE_UTSNAME
  165.             ptr = u.sysname;
  166. #else
  167.             ptr = "";
  168. #endif
  169.             goto get_system_info;
  170.  
  171.         case 'm' :
  172.  
  173. #ifdef HAVE_UTSNAME
  174.             ptr = u.machine;
  175. #else
  176.             ptr = "";
  177. #endif
  178.  
  179.                 get_system_info:
  180.  
  181.                 if (ptr[0])
  182.                 {
  183.                     len = strlen(ptr);
  184.                     temp_msg = xrealloc(temp_msg,
  185.                             temp_msg_len += len);
  186.                     memcpy(&temp_msg[j], ptr, len);
  187.                 }
  188.                 else
  189.                 {
  190.                     len = 6;
  191.                     temp_msg = xrealloc(temp_msg,
  192.                             temp_msg_len += len);
  193.                     memcpy(&temp_msg[j], "(none)", len);
  194.                 }
  195.  
  196.                 j += len;
  197.                 i++;
  198.                 break;
  199.  
  200.         case 'd' :      time = get_local_time();
  201.                 sprintf(date_str, "%s %s %02d %d",
  202.                     day_name[time->tm_wday],
  203.                     month_name[time->tm_mon],
  204.                     time->tm_mday,
  205.                     time->tm_year + 1900);
  206.                 len = strlen(date_str);
  207.                 temp_msg = xrealloc(temp_msg,
  208.                             temp_msg_len += len);
  209.                 memcpy(&temp_msg[j], date_str, len);
  210.                 j += len;
  211.                 i++;
  212.                 break;
  213.  
  214.         case '\\':      temp_msg[j++] = '\\';
  215.                 i++;
  216.                 break;
  217.  
  218.         case '\0':      temp_msg[j++] = '\\';
  219.                 break;
  220.  
  221.         default  :      temp_msg[j++] = '\\';
  222.                 temp_msg[j++] = msg_name[++i];
  223.                 break;
  224.         }
  225.     else
  226.     {
  227.         if (msg_name[i] == '\t')
  228.         {
  229.         temp_msg = xrealloc(temp_msg, temp_msg_len += 8);
  230.         memcpy(&temp_msg[j], "        ", 8);
  231.         j += 8;
  232.         }
  233.         else
  234.         temp_msg[j++] = msg_name[i];
  235.     }
  236.  
  237.     temp_msg[j] = 0;
  238.  
  239.     len = strlen(msg_name = temp_msg);
  240.  
  241.     if (center && len < columns)
  242.     memcpy(stemp + ((columns - len) >> 1), msg_name, len);
  243.     else
  244.     memcpy(stemp, msg_name, min(len, columns));
  245.  
  246.     xfree(temp_msg);
  247.  
  248.     for (i = 0; i < columns; i++)
  249.     if (stemp[i] == '\r' || stemp[i] == '\n')
  250.         stemp[i] = ' ';
  251. }
  252.  
  253.  
  254. int
  255. status(msg_name, wait, sound, restore, msg_type, center)
  256.     char *msg_name;
  257.     int wait, sound, restore, msg_type, center;
  258. {
  259.     int key = 0;
  260.     tty_key_t *ks;
  261.     tty_status_t status;
  262.  
  263.  
  264.     tty_save(&status);
  265.  
  266.     build_msg(msg_name, center);
  267.  
  268.     if (sound)
  269.     tty_beep();
  270.  
  271.     switch (msg_type)
  272.     {
  273.     case MSG_STATUS:
  274.  
  275.         tty_colors(StatusBarWarningBrightness,
  276.                StatusBarWarningForeground,
  277.                StatusBarWarningBackground);
  278.         break;
  279.  
  280.     case MSG_ERROR:
  281.  
  282.         tty_colors(StatusBarErrorBrightness,
  283.                StatusBarErrorForeground,
  284.                StatusBarErrorBackground);
  285.         break;
  286.  
  287.     default:
  288.  
  289.         tty_colors(StatusBarBrightness,
  290.                StatusBarForeground,
  291.                StatusBarBackground);
  292.         break;
  293.     }
  294.  
  295.     window_goto(status_win, 0, 0);
  296.     window_puts(stemp, columns);
  297.     window_update();
  298.  
  299.     if (wait)
  300.     {
  301.     ks = tty_get_key(NULL);
  302.     window_goto(status_win, line, columns);
  303.     key = ks->key_seq[0];
  304.     }
  305.  
  306.     if (restore)
  307.     {
  308.     build_msg(default_msg, MSG_CENTERED);
  309.  
  310.     tty_colors(StatusBarBrightness,
  311.            StatusBarForeground,
  312.            StatusBarBackground);
  313.  
  314.     window_goto(status_win, 0, 0);
  315.     window_puts(stemp, columns);
  316.     window_update();
  317.     }
  318.  
  319.     tty_restore(&status);
  320.     return key;
  321. }
  322.